<script setup>
import { onMounted, ref } from 'vue';
import { useUserStore } from '@/store/user';

const userStore = useUserStore();
const mobileMenuOpen = ref(false);
const dropdownOpen = ref(false);

// 页面加载时获取用户资料
onMounted(async () => {
  if (userStore.token) {
    await userStore.fetchProfile();
  }
});

// 切换移动菜单
const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value;
  // 关闭下拉菜单
  if (!mobileMenuOpen.value) {
    dropdownOpen.value = false;
  }
};

// 关闭移动菜单
const closeMobileMenu = () => {
  mobileMenuOpen.value = false;
  dropdownOpen.value = false;
};

// 切换下拉菜单
const toggleDropdown = (event) => {
  // 在移动设备上阻止默认行为
  if (window.innerWidth <= 768) {
    event.preventDefault();
    dropdownOpen.value = !dropdownOpen.value;
  }
};
</script>

<template>
  <div class="app">
    <header class="app-header">
      <div class="container header-container">
        <router-link to="/" class="logo" @click="closeMobileMenu">
          知识卡片学习系统
        </router-link>
        
        <!-- 移动端菜单按钮 -->
        <button class="mobile-menu-toggle" @click="toggleMobileMenu" aria-label="菜单">
          <span class="hamburger" :class="{ 'active': mobileMenuOpen }"></span>
        </button>
        
        <nav class="main-nav" :class="{ 'mobile-open': mobileMenuOpen }">
          <template v-if="userStore.isAuthenticated">
            <router-link to="/learning-paths" @click="closeMobileMenu">学习路径</router-link>
            <router-link to="/documents" @click="closeMobileMenu">文档</router-link>
            <router-link to="/cards" @click="closeMobileMenu">卡片</router-link>
            <router-link to="/exercises" @click="closeMobileMenu">练习</router-link>
            <div class="dropdown" :class="{ 'active': dropdownOpen }">
              <button class="dropdown-toggle" @click="toggleDropdown">
                {{ userStore.getUsername || '用户' }}
                <span class="dropdown-icon"></span>
              </button>
              <div class="dropdown-menu">
                <router-link to="/profile" @click="closeMobileMenu">个人中心</router-link>
                <router-link to="/bookmarks" @click="closeMobileMenu">书签</router-link>
                <router-link to="/notes" @click="closeMobileMenu">笔记</router-link>
                <router-link to="/progress" @click="closeMobileMenu">学习进度</router-link>
                <a href="#" @click.prevent="userStore.logout(); closeMobileMenu()">退出登录</a>
              </div>
            </div>
          </template>
          <template v-else>
            <router-link to="/login" @click="closeMobileMenu">登录</router-link>
            <router-link to="/register" @click="closeMobileMenu">注册</router-link>
          </template>
        </nav>
      </div>
    </header>
    
    <main class="app-main">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
    
    <footer class="app-footer">
      <div class="container">
        <p>&copy; {{ new Date().getFullYear() }} 知识卡片学习系统 - 版权所有</p>
      </div>
    </footer>
  </div>
</template>

<style>
/* 全局样式 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #f9f9f9;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

a {
  color: #4CAF50;
  text-decoration: none;
}

/* 应用布局 */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 1rem 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  color: #4CAF50;
  margin-right: 1rem;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.main-nav a {
  color: #555;
  font-weight: 500;
  transition: color 0.3s;
}

.main-nav a:hover,
.main-nav a.router-link-active {
  color: #4CAF50;
}

.main-nav a.router-link-active {
  position: relative;
  font-weight: 600;
}

.main-nav a.router-link-active::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #4CAF50;
  transform: scaleX(1);
}

/* 移动菜单按钮 */
.mobile-menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
}

.hamburger {
  display: block;
  width: 24px;
  height: 2px;
  background-color: #333;
  position: relative;
  transition: background-color 0.3s;
}

.hamburger::before,
.hamburger::after {
  content: '';
  position: absolute;
  width: 24px;
  height: 2px;
  background-color: #333;
  transition: transform 0.3s;
}

.hamburger::before {
  top: -6px;
}

.hamburger::after {
  bottom: -6px;
}

.hamburger.active {
  background-color: transparent;
}

.hamburger.active::before {
  transform: rotate(45deg);
  top: 0;
}

.hamburger.active::after {
  transform: rotate(-45deg);
  bottom: 0;
}

/* 下拉菜单 */
.dropdown {
  position: relative;
}

.dropdown-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  color: #555;
  padding: 0.5rem 0;
  display: flex;
  align-items: center;
}

.dropdown-icon {
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 0.5rem;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #555;
  transition: transform 0.3s;
}

.dropdown.active .dropdown-icon {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  min-width: 150px;
  display: none;
  z-index: 1000;
}

/* 桌面端悬停显示下拉菜单 */
@media (min-width: 769px) {
  .dropdown:hover .dropdown-menu {
    display: block;
  }
}

/* 移动端点击显示下拉菜单 */
.dropdown.active .dropdown-menu {
  display: block;
}

.dropdown-menu a {
  display: block;
  padding: 0.75rem 1rem;
  color: #333;
  transition: background-color 0.3s;
}

.dropdown-menu a:hover {
  background-color: #f5f5f5;
}

/* 主内容区 */
.app-main {
  flex: 1;
  padding: 2rem 0;
}

/* 页脚 */
.app-footer {
  background-color: #f5f5f5;
  padding: 2rem 0;
  text-align: center;
  color: #666;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .header-container {
    justify-content: space-between;
    padding: 0 1rem;
  }
  
  .mobile-menu-toggle {
    display: block;
    order: 3;
  }
  
  .logo {
    order: 1;
    margin-right: auto;
  }
  
  .main-nav {
    order: 2;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    width: auto;
    gap: 1rem;
    padding: 0;
  }
  
  .main-nav.mobile-open {
    width: 100%;
    margin-top: 1rem;
    padding: 1rem 0;
    border-top: 1px solid #eee;
  }
  
  .dropdown {
    position: relative;
  }
  
  .dropdown-toggle {
    padding: 0.5rem;
  }
  
  .dropdown-menu {
    position: absolute;
    top: 100%;
    right: 0;
    width: auto;
    min-width: 150px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    border-left: none;
  }
  
  .app-main {
    padding: 1rem 0;
  }
}

/* 小屏幕设备优化 */
@media (max-width: 480px) {
  .logo {
    font-size: 1.2rem;
  }
  
  .main-nav {
    gap: 0.75rem;
  }
  
  .main-nav a {
    font-size: 0.9rem;
    padding: 0.25rem 0.5rem;
  }
  
  .dropdown-toggle {
    font-size: 0.9rem;
    padding: 0.25rem 0.5rem;
  }
  
  .app-main {
    padding: 0.5rem 0;
  }
  
  .app-footer {
    padding: 1rem 0;
    font-size: 0.9rem;
  }
}
</style>
